<?php
/**
 * /depot/library/mvc-helper/ActionHelper/ActionLogin.php
 * @author qqinxl
 * 2008/03/31
 * 
 * @category  
 * @package  
 * @copyright  Copyright (c) {}
 */
class ActionHelper_ActionLogin extends ActionHelper_Abstract_Basic{
	
	/**
	 * @var Module_Interface_Auth
	 * @access protected
	 */
	protected $_module = null;
	
	const OPTIONS_MODULE_AUTH = 'module_auth';
	
	public function setOptions($options){
		parent::setOptions($options);
		if(isset($options[self::OPTIONS_MODULE_AUTH])){
			$module = $options[self::OPTIONS_MODULE_AUTH];
			if (! $module instanceof Module_Interface_Auth) {
				throw new Exception ( 'module must instance of Module_Interface_Auth' );
			}
			$this->_module = $module;
		}
	}
	
	public function process($options = null) {	
		parent::process($options);				
		if (! empty ( $this->_loginUser ) and $this->_loginUser instanceof Object_User and $this->_loginUser->isEnable ()) {
			$this->getActionController ()->getHelper ( 'Redirector' )->gotoUrl ( $this->_pageInfo->getModule() );
			return;
		}
		$this->_formData = null;		
		if ($this->getRequest ()->isPost ()) {
			$this->getActionController ()->getHelper ( 'viewRenderer' )->setNoRender ();
			$this->_formData = $this->getActionController ()->getPosts ();
			if ($this->_form->isValid ( $this->_formData )) {			
				$check = $this->_module->checkAuth ( $this->_formData [$this->_module->getCheckAuthBy()], $this->_formData ['password'] );
				$options = $this->_changeI18nTranslate ();
				if (empty ( $check )) {
					$this->view->errors = $this->_translator->translateError ( $options );
					$this->view->message = $this->_translator->_ ( $this->_i18nMsg [self::FAIL] );
					$this->getActionController ()->getHelper ( 'message' )->set ( Htmlhelper_Img::IMG_FAIL );
				} else {
					$this->view->loginSuccess = true;
					$this->_module->setSession ( $check ,true);
					$this->view->message = $this->_translator->_ ( $this->_i18nMsg [self::SUCCESS], $options );
					$this->getActionController ()->getHelper ( 'message' )->set ( Htmlhelper_Img::IMG_SUCCESS );
				}
			} else {
				$this->view->message = $this->_translator->_ ( $this->_i18nMsg [self::INPUT] );
				$this->view->errors = $this->_form->translateError ();
				$this->getActionController ()->getHelper ( 'message' )->set ();
			}
			return;
		}
		$this->getActionController ()->render ();
	}	
}